package com.miplus.ccrm.core.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.miplus.ccrm.core.pojo.CfgRuleVars;

public enum EnumRuleVarsOp {
    /**
     * 比较ID
     * 1 = 等于
     * 2 != 不等于
     * 3 like 含有
     * 4 like 开始于
     * 5 like 终于
     * 6  > 大于
     * 7  < 小于
     * 8 >= 大于等于
     * 9  <= 小于等于
     * 10 in 包括
     * 11 not in 不包括
     * 12 exists 存在
     * 13 not exists 不存在
     */
    EQUAL(1, "等于"),
    NOTEQUAL(2, "不等于"),
    LIKE(3, "含有"),
    LEFTLIKE(4, "开始于"),
    RIGHTLIKE(5,"终于"),
    GREAT(6,"大于"),
    LITTLE(7,"小于"),
    GREATEQUAL(8,"大于等于"),
    LITTLEEQUAL(9,"小于等于"),
    IN(10,"包含"),
    NOTIN(11,"不包含"),
    ISNOTNULL(12,"不为空"),
    ISNULL(13,"为空");
    private final Integer code;
    private final String desc;

    // 构造方法 ,赋值给成员变量
    EnumRuleVarsOp( int code,String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode()
    {
        return code;
    }
    public String getDesc()
    {
        return desc;
    }
    public static String getDescByCode(Integer code) {
        for (EnumRuleVarsOp enums : EnumRuleVarsOp.values()) {
            if (enums.getCode().equals(code)) {
                return enums.getDesc();
            }
        }
        return null;
    }
    public static EnumRuleVarsOp getEnumByCode(Integer code) {
        for (EnumRuleVarsOp enums : EnumRuleVarsOp.values()) {
            if (enums.getCode().equals(code)) {
                return enums;
            }
        }
        return null;
    }

    public static String getOpSqlByCode(Integer code, CfgRuleVars vars, String val) {
        String opSql = "";
        String fieldSql = "";
        opSql += StringUtils.isEmpty(vars.getBeforeCondition()) ? "" : vars.getBeforeCondition();

        fieldSql = StringUtils.isEmpty(vars.getBeforeField()) ? "" : vars.getBeforeField();
        fieldSql +=  " " + vars.getTableAlias() + "." + vars.getFieldName();
        fieldSql +=  StringUtils.isEmpty(vars.getAfterField()) ? "" : vars.getAfterField();
        EnumRuleVarsOp enumRuleVarsOp = getEnumByCode(code);
        switch(enumRuleVarsOp){
            case EQUAL:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " = '" + val + "'";
                }
                else {
                    fieldSql +=  " = " + val;
                }
                 break;

            case NOTEQUAL:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " != '" + val + "'";
                }
                else {
                    fieldSql +=  " != " + val;
                }
                break;
            case LIKE:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " like '%" + val + "%'";
                }
                break;
            case LEFTLIKE:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += "  like '" + val + "%'";
                }
                break;
            case RIGHTLIKE:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " like '%" + val + "'";
                }
                break;
            case GREAT:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " > '" + val + "'";
                }
                else {
                    fieldSql +=  " > " + val;
                }
                break;
            case LITTLE:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " < '" + val + "'";
                }
                else {
                    fieldSql +=  " < " + val;
                }
                break;
            case GREATEQUAL:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " >= '" + val + "'";
                }
                else {
                    fieldSql +=  " >= " + val;
                }
                break;
            case LITTLEEQUAL:
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    fieldSql += " <= '" + val + "'";
                }
                else {
                    fieldSql +=  " <= " + val;
                }
                break;
            case IN:
                if(StringUtils.isEmpty(val))
                    return "";
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {
                    String vStrlist  = val.replace(",","','");
                    fieldSql += " in ('" + vStrlist + "')";
                }
                else {
                    fieldSql +=  " in (" + val + ")";
                }
                break;
            case NOTIN:
                if(StringUtils.isEmpty(val))
                    return "";
                if( vars.getFieldType().equalsIgnoreCase("string"))
                {

                    String vStrlist  = val.replace(",","','");
                    fieldSql += " not in ('" + vStrlist + "')";
                }
                else {
                    fieldSql +=  " not in (" + val + ")";
                }
                break;
            case ISNULL:
                if( vars.getFieldType().equalsIgnoreCase("string")) {
                    fieldSql = "IFNULL(" + fieldSql + ",'') = ''";
                }
                else
                {
                    fieldSql += " is null";
                }
                break;
            case ISNOTNULL:
                if( vars.getFieldType().equalsIgnoreCase("string")) {
                    fieldSql = "IFNULL(" + fieldSql + ",'') != ''";
                }
                else
                {
                    fieldSql += " is not null";
                }
                break;
//            case EXISTS:
//                if(StringUtils.isEmpty(val))
//                    return "";
//                if( vars.getFieldType().equalsIgnoreCase("string"))
//                {
//                    String vStrlist  = val.replace(",","','");
//                    opSql = " exists (" + opSql + " in ( '" + vStrlist +"')";
//                }
//                else {
//
//                    opSql = " exists (" + opSql + " in (" + val + ")";
//                }
//                break;
//            case NOTEXISTS:
//                if(StringUtils.isEmpty(val))
//                    return "";
//                if( vars.getFieldType().equalsIgnoreCase("string"))
//                {
//                    String vStrlist  = val.replace(",","','");
//                    opSql = " not exists " + opSql + " in ( '" + vStrlist +"')";
//                }
//                else {
//
//                    opSql = " not exists " + opSql + " in (" + val + ")";
//                }
//                break;
        }
        opSql += " " + fieldSql + " ";
        if(StringUtils.isNotEmpty(vars.getAfterCondition()) )
            opSql += vars.getAfterCondition();
        return opSql;
    }
}
